<?php

namespace Biz\model\course;

use Doraemon\pockets\datebase\BaseDbModel;

class CourseIapReceipt extends BaseDbModel
{

    const _CONFIG_FILE_ = 'db_course';

    const _CONFIG_SELECT_ = 'db_course';

    const _DATABASE_ = 'hhz_course';

    const _TABLE_NAME = 'course_iap_receipt';

    public static function addInfo($orderNo, $receipt = null, $appleGid = null, $payAmount = null, $receiptInfo = null)
    {
        $info = self::getByWhere(['order_no' => $orderNo]);
        if ($info) {
            $data = [
                'order_no' => $orderNo,
            ];
            if (!is_null($appleGid) && empty($info['apple_gid'])) {
                $data['apple_gid'] = $appleGid;
            }
            if (!is_null($payAmount) && $info['pay_amount'] <= 0) {
                $data['pay_amount'] = $payAmount;
            }
            if (!is_null($receiptInfo)) {
                $data['receipt_info'] = $receiptInfo;
            }
            return self::updateData($data, ['order_no' => $orderNo]);
        } else {
            $data = [
                'order_no' => $orderNo,
            ];
            if (!is_null($appleGid) && empty($info['apple_gid'])) {
                $data['apple_gid'] = $appleGid;
            }
            if (!is_null($payAmount) && $info['pay_amount'] <= 0) {
                $data['pay_amount'] = $payAmount;
            }
            if (!is_null($receiptInfo)) {
                $data['receipt_info'] = $receiptInfo;
            }
            $receiptId = self::insertData($data);
            if ($receiptId && !is_null($receipt)) {
                CourseIapReceiptData::addInfo($receiptId, $receipt);
            }
        }
    }

    public static function getReceiptInfo($orderNo)
    {
        $info = self::getByWhere(['order_no' => $orderNo]);
        if (!empty($info)) {
            $info['receipt_info'] = $info['receipt_info'] ? json_decode($info['receipt_info'], true) : [];
            $receiptData = CourseIapReceiptData::getByWhere(['id' => $info['id']]);
            $info['receipt_data'] = $receiptData['receipt_data'];
        }
        return $info;
    }

    public static function selectByWhere($where, $select = '*')
    {
        $list = self::select($select, $where);
        return $list;
    }

    public static function getByWhere($where)
    {
        return self::get('*', $where);
    }

    public static function selectCount($where)
    {
        return self::count($where);
    }

    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }

    public static function updateData($data, $where)
    {
        $data['edittime'] = date('Y-m-d H:i:s');
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

}

